using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Xeml.Document;

namespace XemlInteractiveDesigner
{
    public partial class GermPlasmEditForm : Form
    {
        public IndividualsPool IndividualsPool { get; private set; }

        public GermPlasmEditForm(IndividualsPool ip)
        {
            InitializeComponent();
            if (ip == null)
                this.IndividualsPool = new IndividualsPool();
            else
            {
                this.IndividualsPool = ip;
                this.germPlasmEditCtrl.GermPlasmId = ip.GermPlasm;
                if (ip.Annotations.Contains("FreeText"))
                    this.germPlasmEditCtrl.FreeText = ip.Annotations.Find("FreeText").Annotation;
                if (ip.Annotations.Contains("NcbiTaxonomyId"))
                    this.germPlasmEditCtrl.NcbiTaxonomyId = ip.Annotations.Find("NcbiTaxonomyId").Annotation;
            }
            this.germPlasmEditCtrl.IndividualsPool = this.IndividualsPool;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (ValidateChildren())
            {
                this.IndividualsPool.NameSpace = "none";
                this.IndividualsPool.GermPlasm = germPlasmEditCtrl.GermPlasmId;

                if (String.IsNullOrEmpty(germPlasmEditCtrl.FreeText))
                    IndividualsPool.Annotations.Remove("FreeText");
                else
                {
                    if (IndividualsPool.Annotations.Contains("FreeText"))
                        this.IndividualsPool.Annotations.Find("FreeText").Annotation = germPlasmEditCtrl.FreeText;
                    else
                        this.IndividualsPool.Annotations.Add(new TaggedAnnotation("FreeText", germPlasmEditCtrl.FreeText));
                }

                if (String.IsNullOrEmpty(germPlasmEditCtrl.NcbiTaxonomyId))
                    IndividualsPool.Annotations.Remove("NcbiTaxonomyId");
                else
                {
                    if (IndividualsPool.Annotations.Contains("NcbiTaxonomyId"))
                        this.IndividualsPool.Annotations.Find("NcbiTaxonomyId").Annotation = germPlasmEditCtrl.NcbiTaxonomyId;
                    else
                        this.IndividualsPool.Annotations.Add(new TaggedAnnotation("NcbiTaxonomyId", germPlasmEditCtrl.NcbiTaxonomyId));
                }

                // this.individualsPool. 
                //TODO: PoolSize management
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}